<?php

namespace app\common\model;

class Deal extends BaseModel
{
    public function getDeal($bisId)
    {
        return $this->where(['bis_id' => $bisId])->select();
    }

    public function getDealByStatus($status = 0)
    {
        $data = [
            'status' => $status
        ];
        return $this->where($data)->select();
    }

    //进行查询
    public function getNormalDeals($data = [])
    {
//        $data['status'] = 1;
        $order = ['id' => 'desc'];
        $result = $this->where($data)->order($order)->paginate(2);
        echo $this->getLastSql();
        return $result;
    }

    //前台显示促销商品
    public function getNormalDealByCategoryCityId($catid, $cityid, $limit = 10)
    {
        $time = date('Y-m-d H:i:s');
        $data = [
            "end_time" => ["gt", $time],
            "category_id" => $catid,
            'status' => 1
        ];
        $order = [
            'listorder' => 'desc',
            'id' => 'desc'
        ];
        $require = $this->where($data)->where("FIND_IN_SET($cityid,city_id)")->order($order);
        if ($limit) {
            $require = $require->limit($limit)->select();
        } else {
            $require = $require->select();
        }
        echo $this->getLastSql();
//        dump($require);
        return $require;
    }

    //根据排序条件进行排序
    public function getDealByCategoryAndOrder($data, $order)
    {
        $datas = [];
        if (!empty($data['city_id'])) {
            $datas[] = "FIND_IN_SET('" . $data['city_id'] . "',city_id)";
        }
        if (!empty($data['se_category_id'])) {
            $datas[] = "FIND_IN_SET('" . $data['se_category_id'] . "',se_category_id)";
        }
        if (!empty($data['category_id'])) {
            $datas[] = "category_id = " . $data['category_id'];
        }
        $datas[] = "end_time > '" . date('Y-m-d H:i:s') . "'";
        $datas[] = 'status = 1';
        $res = $this->where(implode(' AND ', $datas))->order($order)->paginate(1);
        echo $this->getLastSql();
        return $res;
    }

    //支付成功，更新购买数量
    public function updateBuyCountById($id, $count)
    {
        return $this->allowField(true)->where(['id' => $id])->setInc('buy_count', $count);
    }

    //支付成功,更新商品数量
    public function updateTotalCountById($id, $count)
    {
        return $this->allowField(true)->where(['id' => $id])->setDec('total_count', $count);
    }
}
